﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;

namespace WuDada.Core.Post.Persistence
{
    public class AttachedFileDao : AdoDao, IAttachedFileDao
    {
        public INHibernateDao NHibernateDao { get; set; }                

        /// <summary>
        /// 新增AttachedFile
        /// </summary>
        /// <param name="attachedFileVO">被新增的AttachedFile</param>
        /// <returns>新增後的AttachedFile</returns>
        public AttachedFileVO CreateAttachedFile(AttachedFileVO attachedFileVO)
        {
            NHibernateDao.Insert(attachedFileVO);

            return attachedFileVO;
        }

        /// <summary>
        /// 取得AttachedFile By AttachedFileId
        /// </summary>
        /// <param name="attachedFileId">AttachedFileId</param>
        /// <returns>AttachedFile</returns>
        public AttachedFileVO GetAttachedFileById(int attachedFileId)
        {
            return NHibernateDao.GetVOById<AttachedFileVO>(attachedFileId);
        }

        /// <summary>
        /// 取得AttachedFile By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="fileType">檔案類別</param>
        /// <returns>AttachedFile清單</returns>
        public IList<AttachedFileVO> GetAttachedFileListByPostId(int postId, AttachedFileVO.Type fileType)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AttachedFileVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));
            dCriteria.Add(Expression.Eq("FileType", (int)fileType));
            dCriteria.AddOrder(Order.Asc("SortNo"));
            dCriteria.AddOrder(Order.Desc("UpdatedDate"));

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<AttachedFileVO>(dCriteria);
        }

        /// <summary>
        /// 刪除AttachedFile
        /// </summary>
        /// <param name="attachedFileVO">被刪除的AttachedFile</param>
        public void DeleteAttachedFile(AttachedFileVO attachedFileVO)
        {
            NHibernateDao.Delete(attachedFileVO);
        }

        /// <summary>
        /// 更新AttachedFile
        /// </summary>
        /// <param name="attachedFileVO">被更新的AttachedFile</param>
        /// <returns>更新後的AttachedFile</returns>
        public AttachedFileVO UpdateAttachedFile(AttachedFileVO attachedFileVO)
        {
            NHibernateDao.Update(attachedFileVO);

            return attachedFileVO;
        }

        /// <summary>
        /// 取得AttachedFile By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="onlyShow">僅抓取上架</param>
        /// <param name="fileType">檔案類別</param>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>AttachedFile清單</returns>
        public IList<AttachedFileVO> GetAttachedFileListByPostId(int postId, bool onlyShow, AttachedFileVO.Type fileType, string sortField, bool sortDesc)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AttachedFileVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));
            dCriteria.Add(Expression.Eq("FileType", (int)fileType));
            
            if (onlyShow)
            {
                dCriteria.Add(Expression.Eq("Flag", 1));                
            }

            if (!string.IsNullOrEmpty(sortField))
            {
                if (sortDesc)
                {
                    dCriteria.AddOrder(Order.Desc(sortField));
                }
                else
                {
                    dCriteria.AddOrder(Order.Asc(sortField));
                }
            }

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<AttachedFileVO>(dCriteria);
        }

        /// <summary>
        /// 取得AttachedFile By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="onlyShow">僅抓取上架</param>
        /// <param name="fileType">檔案類別</param>
        /// /// <param name="startDate">起始的上架日期</param>
        /// <param name="pageIndex">分頁索引</param>
        /// <param name="pageSize">分頁大小</param>
        /// <param name="sortField">排序欄位</param>
        /// <param name="sortDesc">升降冪排序</param>
        /// <returns>AttachedFile清單</returns>
        public IList<AttachedFileVO> GetAttachedFileListByPostId(int postId, bool onlyShow, AttachedFileVO.Type fileType, DateTime? startShowDate, int pageIndex, int pageSize, string sortField, bool sortDesc)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AttachedFileVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));
            dCriteria.Add(Expression.Eq("FileType", (int)fileType));
            
            if (onlyShow)
            {
                dCriteria.Add(Expression.Eq("Flag", 1));
            }

            if (startShowDate != null)
            {
                dCriteria.Add(Expression.Le("ShowDate", startShowDate));
            }

            if (!string.IsNullOrEmpty(sortField))
            {
                if (sortDesc)
                {
                    dCriteria.AddOrder(Order.Desc(sortField));
                }
                else
                {
                    dCriteria.AddOrder(Order.Asc(sortField));
                }
            }

            int count = NHibernateDao.CountByDetachedCriteria(dCriteria);

            if (count == 0)
            {
                return null;
            }

            return NHibernateDao.SearchByDetachedCriteria<AttachedFileVO>(dCriteria, pageIndex, pageSize);
        }

        /// <summary>
        /// 取得AttachedFile筆數 By PostId
        /// </summary>
        /// <param name="postId">PostId</param>
        /// <param name="onlyShow">僅抓取上架</param>
        /// <param name="fileType">檔案類別</param>
        /// /// <param name="startDate">起始的上架日期</param>
        /// <returns>AttachedFile清單</returns>
        public int CountAttachedFileListByPostId(int postId, bool onlyShow, AttachedFileVO.Type fileType, DateTime? startShowDate)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AttachedFileVO>();
            dCriteria.CreateCriteria("Post").Add(Expression.Eq("PostId", postId));
            dCriteria.Add(Expression.Eq("FileType", (int)fileType));

            if (onlyShow)
            {
                dCriteria.Add(Expression.Eq("Flag", 1));
            }

            if (startShowDate != null)
            {
                dCriteria.Add(Expression.Le("ShowDate", startShowDate));
            }

            return NHibernateDao.CountByDetachedCriteria(dCriteria);
        }        
    }
}
